package com.ibeetl.admin.console.web.stat;

import com.ibeetl.admin.console.service.stat.RegistStatService;
import com.ibeetl.admin.console.service.stat.ReportStatService;
import com.ibeetl.admin.console.web.query.stat.RegistStatQuery;
import com.ibeetl.admin.console.web.query.stat.ReportStatQuery;
import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.annotation.Query;
import com.ibeetl.admin.core.entity.stat.RegistStat;
import com.ibeetl.admin.core.entity.stat.ReportStat;
import com.ibeetl.admin.core.file.FileItem;
import com.ibeetl.admin.core.file.FileService;
import com.ibeetl.admin.core.util.AnnotationUtil;
import com.ibeetl.admin.core.util.PlatformException;
import com.ibeetl.admin.core.web.JsonResult;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.beetl.sql.core.engine.PageQuery;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * 报告查询次数汇总统计
 * 
 * @author xiandafu
 */
@Controller
public class RegistStatController {
	private final Log log = LogFactory.getLog(this.getClass());
	private static final String MODEL = "/admin/registStat";
	@Autowired
	private RegistStatService registStatService;
	@Autowired
	FileService fileService;


   

	/* 页面 */

	@GetMapping(MODEL + "/index.do")
	@Function("registStat")
	public ModelAndView index() {
		ModelAndView view = new ModelAndView("/admin/stat/registStat/index.html");
		view.addObject("search", RegistStatQuery.class.getName());
		return view;
	}

	@PostMapping(MODEL + "/list.json")
	@Function("registStat.query")
	@ResponseBody
	public JsonResult<PageQuery<RegistStat>> index(RegistStatQuery condtion) {

		PageQuery<RegistStat> page = condtion.getPageQuery();
		registStatService.queryByCondtion(page);
		return JsonResult.success(page);
	}

	@PostMapping(MODEL + "/list/condition.json")
	@Function("registStat.query")
	@ResponseBody
	public JsonResult<List<Map<String, Object>>> indexCondtion() {
		List<Map<String, Object>> list = AnnotationUtil.getInstance().getAnnotations(Query.class, RegistStatQuery.class);
		return JsonResult.success(list);
	}


	@PostMapping(MODEL + "/excel/export.json")
	@Function("reportStat.export")
	@ResponseBody
	public JsonResult<String> export(HttpServletResponse response, RegistStatQuery condtion) {
		String excelTemplate ="excelTemplates/admin/stat/registStat_collection_template.xls";
		PageQuery<RegistStat> page = condtion.getPageQuery();
		//取出全部符合条件的
		page.setPageSize(Integer.MAX_VALUE);
		page.setPageNumber(1);
		page.setTotalRow(Integer.MAX_VALUE);
		List<RegistStat> registStats =registStatService.queryExcel(page);
		try(InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(excelTemplate)) {
			if(is==null) {
				throw new PlatformException("模板资源不存在："+excelTemplate);
			}
			FileItem item = fileService.createFileTemp("registStat_collection.xls");
			OutputStream os = item.openOutpuStream();
			Context context = new Context();
			context.putVar("registStats", registStats);
			JxlsHelper.getInstance().processTemplate(is, os, context);
			//下载参考FileSystemContorller
			return  JsonResult.success(item.getPath());
		} catch (IOException e) {
			throw new PlatformException(e.getMessage());
		}

	}





}
